let _util = require('util/util.js');
let _index = require('service/indexService.js');
let navSide = require('../aside/index.js');
let ImageModal = require('util/imageModal/index.js');

let page = {
    data: {
        imageModal: null
    },
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function () {
        _index.loadTableInfo({
            //表头字段
            fields: {
                "fileNo": "原所属档号",
                "positionNo": "原所属位置",
                "names": "逝者姓名",
                "documents": "相关证件",
                "opera": "相关操作"
            },
            //格式化数据
            formatterData: function (data, fields) {
                let formatterArray = [];

                $.each(data, function (index, value) {
                    let newObj = Object.assign({}, data[index]);
                    newObj.documents = `<a href="#" data-fileId="${value.id}" class="jsOpenImageModal">点击查看</a>`;
                    // newObj.opera = `
                    //     <a href="#" class="jsAddNew opera">
                    //         <i class="fa fa-plus" style="color:green"></i>
                    //     </a>
                    //     <a href="#" class="jsEdit opera">
                    //         <i class="fa fa-pencil"></i>
                    //     </a>
                    //     <a href="#" class="jsAddToTrash opera" data-fileId="${value.id}">
                    //         <i class="fa fa-trash"></i>
                    //     </a>
                    //     <a href="#" class="jsRemove" data-fileId="${value.id}">
                    //         <i class="fa fa-times" ></i>
                    //     </a>
                    // `
                    newObj.opera = `
                        <a href="#" class="jsEdit opera" title="撤销删除">
                            <i class="fa fa-pencil"></i>
                        </a>
                        <a href="#" class="jsRemove" data-fileId="${value.id}" title="彻底删除">
                            <i class="fa fa-times" style="color:red"></i>
                        </a>
                    `
                    formatterArray.push(newObj);
                })

                return formatterArray;
            }
        })
            .then((res) => {
                _util.renderTable({
                    fields: res.params.fields || {},
                    formatterData: res.params.formatterData,
                    selector: $('#info_table'),
                    data: res
                });
            })
            ;

        navSide.init({
            name: 'trash-file'
        })
    },
    bindEvent: function () {
        let _this = this;

        //删除目录
        $(document).on('click', '.jsRemove', function (e) {
            let $target = $(e.currentTarget);
            if (window.confirm('真的要删除该条目录？此操作不可逆！')) {
                console.log($target.data('fileid'));
            }
        });

        $(document).on('click', '.jsRecover', function (e) {
            let $target = $(e.currentTarget);
            if (window.confirm('确定要恢复此目录？')) {
                console.log($target.data('fileid'));
            }
        });

        //查看证件照
        $(document).on('click', '.jsOpenImageModal', function () {

            let fileid = $(this).data('fileid').split('-');

            _this.data.imageModal ? '' : (_this.data.imageModal = new ImageModal());

            _index
                .loadImages({
                    data: {
                        boxNo: fileid[0],
                        fileNo: fileid[1],
                        positionNo: fileid[2]
                    }
                })
                .then(res => {
                    _this.data.imageModal.render({
                        container: $('.image-modal-wrapper'),
                        data: res[0] || {}
                    });
                    $('.image-modal-wrapper').show();
                })


        })

    }
}

$(function () {
    page.init();
})